package servlet;


import beans.Pizza;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import metier.Produit;
import dao.DAOFactory;

/**
 * Servlet implementation class Hello
 */
@WebServlet({ "/Hello", "/Index" })
public class Index extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Index() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		PrintWriter out = response.getWriter();
			
		response.setContentType("text/html");

		//Debut du formulaire
		out.println("<form method=\"post\" " +
			"action=\""+response.encodeUrl(request.getContextPath()+"/Acheter\">"));
		 
		//Impression de la liste des produits
		List<Pizza> lesProduits = DAOFactory.getDAOPizza().findAll();
		
		out.println("<table style='border: 1px black solid;'>");
		out.println("<tr><td><b>Nom</b></td><td><b>Prix</b></td><td><b>Quantite</b></td></tr>");
		for(Produit unProduit : lesProduits) {
			out.println("<tr>");
			out.println("<td>"+unProduit.getNom()+"</td>");
			out.println("<td>"+unProduit.getPrix()+"</td>");
			out.println("<td>"+
			"<input type=\"hidden\" name=\"idProduit\" value=\""+unProduit.getId()+"\"/>"+
			"<input type=\"text\" name=\"quantite\"/>"
					   +"</td>");
			out.println("</tr>");
		}
		out.println("</table>");
		
		///Fin du formulaire
		out.println("<input type=\"submit\" value=\"Valider\"/>");
		out.println("</form>");

		//Affichage Panier
		RequestDispatcher requestPanier = request.getRequestDispatcher("/PrintPanier");
		if(requestPanier != null) {
			requestPanier.include(request, response);
		}
		

		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}


}
